package com.yilin.platform.service.impl.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yilin.platform.base_core.application.PageData;
import com.yilin.platform.base_core.tools.QueryUtil;
import com.yilin.platform.entity.user.Account;
import com.yilin.platform.entity.user.Cooperate;
import com.yilin.platform.mapper.user.CooperateyMapper;
import com.yilin.platform.service.user.ICooperateervice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 认证服务
 *
 * @author zhangjinyuan
 * @date 2022/10/26
 */
@Service
@Transactional
public class CooperateService extends ServiceImpl<CooperateyMapper, Cooperate> implements ICooperateervice {
    @Resource
    private CooperateyMapper cooperateMapper;
    //申请合作
    @Override
    public void   applyCooperate(Account account, Cooperate cooperate){
        cooperate.setAccountId(account.getId());
        cooperate.setAccountName(account.getName());
        cooperate.setCooperateState(Cooperate.CooperateState.wait);
        saveOrUpdate(cooperate);
    }
    //是否同意
    @Override
    public void   agreeCooperate(Account account, Cooperate cooperate){
        cooperate.setCompanyId(account.getId());
        cooperate.setCompanyName(account.getName());
        saveOrUpdate(cooperate);
    }

    @Override
    public Cooperate   getCooperateById(String id){
      return cooperateMapper.selectById(id);
    }
    @Override
    public Cooperate   getCooperateByCompanyId(String organizeId,String companyId){
        QueryWrapper<Cooperate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_organize_id",companyId);
        queryWrapper.eq("account_organize_id",organizeId);
       return cooperateMapper.selectOne(queryWrapper);
    }
    @Override
    public  void deleteCooperateById(String id){
        cooperateMapper.deleteById(id);
    }
    @Override
    public PageData<Cooperate> getCooperatePage(String organizeId, Cooperate query, Integer page, Integer limit) {
        QueryWrapper<Cooperate> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(wrapper ->
                wrapper.eq("account_organize_id", organizeId)
                        .or()
                        .eq("company_organize_id", organizeId)
        );
        QueryUtil.andQueryWrapper(queryWrapper,query);
        return cooperateMapper.selectPage(new PageData<>(page,limit), queryWrapper);
    }


}
